$(function () {
    //变量抽取
    var role_dataGrid = $("#role_dataGrid");
    var role_dialog = $("#role_dialog");
    var role_form = $("#employee_form");


    role_dataGrid.datagrid({
        url: "/role/query.do",
        fitColumns: true,//列自适应
        fit: true,
        columns: [[
            {field: 'name', title: '角色名称', width: 100,align:'center'},
            {field: 'sn', title: '角色编号', width: 100,align:'center'},
        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true//只允许选择一行
    })

    $("#role_dialog").dialog({
        width: 500,
        height: 460,
        buttons: "#btns",
        closed: true,
        onClose: function () {
            $("#role_form").form("clear");//清空表单
            selfPermission.datagrid('loadData', []);//清空已有权限
            allPermission.datagrid('load');//重新加载所有权限
        },
        resizeable:true
    })

    var allPermission = $("#allPermission");
    var selfPermission = $("#selfPermission");

    allPermission.datagrid({
        title: '所有权限',
        height: 320,
        fitColumns: true,
        rownumbers: true,
        url: "/permission/selectAll.do",
        columns: [[{
            field: 'name', title: '权限名', width: 100, align: 'center'
        }]],
        onClickRow: function (index, row) {
            selfPermission.datagrid("appendRow", row);
            allPermission.datagrid("deleteRow", index);
        }
    });
    selfPermission.datagrid({
        title: '拥有权限',
        height: 320,
        fitColumns: true,
        rownumbers: true,
        url: "/permission/selectPermissionByRoleId.do",
        columns: [[{
            field: 'name', title: '权限名', width: 100, align: 'center'
        }]],
        onClickRow: function (index, row) {
            allPermission.datagrid("appendRow", row);
            selfPermission.datagrid("deleteRow", index);
        },
        //表格加载完成后
        onLoadSuccess: function (data) {
            //获取到已拥有权限的所有权限的ID
            var ids = $.map(data.rows, function (item) {
                return item.id;
            })
            var rows = allPermission.datagrid("getRows");
            for (var i = rows.length - 1; i >= 0; i--) {
                if ($.inArray(rows[i].id, ids) >= 0) {
                    allPermission.datagrid("deleteRow", i);
                }
            }
        }
    });

    //方法抽成对象统一管理
    var methodObj = {//添加按钮
        add: function () {
            $("#role_dialog").dialog("open");
            $("#role_dialog").dialog("setTitle", "添加");
        },
        //编辑按钮
        edit: function () {
            var row = role_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '选择需要编辑的数据', 'info');
                return;
            } else {
                //已拥有权限的回显
                selfPermission.datagrid("load", {roleId: row.id});
                $("#role_form").form("load", row);
                $("#role_dialog").dialog("setTitle", "编辑");
                $("#role_dialog").dialog("open");
                $.get('/role/getMenuIdByRoleId.do', {roleId: row.id}, function (data) {
                    $("#menu_combotree").combotree("setValues", data);
                })
            }
        },
        del: function () {
            var row = role_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("提示", "选择要删除的数据", "info");
            } else {
                $.messager.confirm("提示", "是否要删除该条数据", function (verdict) {
                    if (verdict) {
                        $.get('/role/delete.do', {id: row.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('提示', '删除成功', 'info', function () {
                                    $("#role_dialog").dialog("close");
                                    role_dataGrid.datagrid("reload");
                                })
                            } else {
                                $.messager.alert("提示", "删除失败", 'error');
                            }
                        });
                    }
                })
            }
        },
        reload: function () {
            role_dataGrid.datagrid("load");
        },
        save: function () {
            $("#role_form").form("submit", {
                url: "/role/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data) {
                        $.messager.alert('提示', '保存成功', 'info', function () {
                            $("#role_dialog").dialog("close");
                            role_dataGrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('提示', '保存失败', 'info');
                    }
                },
                onSubmit: function (param) {
                    //获取到已有权限中的所有数据
                    var rows = selfPermission.datagrid('getRows');
                    for (var i = 0; i < rows.length; i++) {
                        //提交参数   param[参数名]=参数值
                        param['permissions[' + i + '].id'] = rows[i].id;
                    }
                    var menus = $("#menu_combotree").combotree("getValues");
                    for (var i = 0; i < menus.length; i++) {
                        param['menus[' + i + '].id'] = menus[i];

                    }
                }
            })
        },
        //取消按钮
        cancel: function () {
            $("#role_dialog").dialog("close")
        }
    }

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });


});


